package com.yunhe.abnormal.controller.base;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.PageParam;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.base.AlarmRuleDTO;
import com.yunhe.abnormal.service.base.AlarmRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author liuronglei
 */
@Api(value = "alarmRule", description = "异常规则", tags = {"Base_AlarmRule"})
@RestController
@RequestMapping("/alarmRules")
public class AlarmRuleController {
    @Autowired
    private AlarmRuleService alarmRuleService;

    /**
     * 新增异常规则对象
     * @param alarmRule 异常规则对象
     */
    @ApiOperation("新增异常规则对象")
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ResultObject> create(@RequestBody AlarmRuleDTO alarmRule)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        AlarmRuleDTO newAlarmRule = alarmRuleService.create(alarmRule);
        return new ResponseEntity<>(new ResultObject<>(newAlarmRule), HttpStatus.OK);
    }

    /**
     * 批量新增异常规则对象
     * @param alarmRules 异常规则对象列表
     */
    @ApiOperation("新增异常规则对象")
    @RequestMapping(value = "/batch",method = RequestMethod.POST)
    public ResponseEntity<ResultObject> create(@RequestBody List<AlarmRuleDTO> alarmRules)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        List<AlarmRuleDTO> newAlarmRules = alarmRuleService.create(alarmRules);
        return new ResponseEntity<>(new ResultObject<>(newAlarmRules), HttpStatus.OK);
    }

    /**
     * 根据异常规则ID删除异常规则
     * @param id 异常规则ID
     */
    @ApiOperation("根据异常规则ID删除异常规则")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> delete(@PathVariable("id") Long id) throws ObjectNotFoundException {
        alarmRuleService.deleteById(id);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    /**
     * 根据异常规则ID批量获得异常规则
     * @param ids 异常规则ID
     */
    @ApiOperation("根据异常规则ID批量获得异常规则")
    @ApiImplicitParam(name = "ids", value = "异常规则ID", required = true, dataType = "long", paramType = "query")
    @RequestMapping(value = "/batch", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> batchDelete(@RequestParam("ids") List<Long> ids) throws ObjectNotFoundException {
        alarmRuleService.batchDelete(ids);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    /**
     * 更新异常规则
     * @param id 异常规则ID
     * @param alarmRule 异常规则
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ApiOperation("更新异常规则")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异常规则ID", required = true, dataType = "long", paramType = "path")
    })
    public ResponseEntity update(@PathVariable("id") Long id,
                                 @RequestBody AlarmRuleDTO alarmRule)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        alarmRule.setId(id);
        AlarmRuleDTO newAlarmRule = alarmRuleService.update(alarmRule);
        return new ResponseEntity<>(new ResultObject<>(newAlarmRule), HttpStatus.OK);
    }

    /**
     * 批量更新异常规则级别
     * @param ids 异常规则ID
     * @param alarmLevelId 异常级别ID
     */
    @ApiOperation("批量更新异常规则级别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "异常规则ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "alarmLevelId", value = "异常级别ID", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/batch", method = RequestMethod.PATCH)
    public ResponseEntity<ResultObject> updateAlarmLevel(@RequestParam("ids") List<Long> ids,
                                                         @RequestParam("alarmLevelId") Long alarmLevelId)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        alarmRuleService.updateAlarmLevel(ids, alarmLevelId);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    /**
     * 根据异常规则ID获得异常规则
     * @param id 异常规则ID
     */
    @ApiOperation("根据异常规则ID获得异常规则")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> get(@PathVariable("id") Long id) {
        AlarmRuleDTO alarmRule = alarmRuleService.findById(id);
        return new ResponseEntity<>(new ResultObject<>(alarmRule), HttpStatus.OK);
    }

    /**
     * 根据条件获得异常规则列表
     * @param queryStr 关键字
     * @param stationTypeId 电站类型ID
     * @param stationId 电站ID
     * @param alarmLevelId 异常级别ID
     * @param deviceTypeId 设备类型ID
     * @param alarmTypeId 异常类型ID
     * @param alarmScopeId 作用范围ID
     * @param dtime 维护时间
     * @param pageParam 分页对象
     */
    @ApiOperation("根据条件获得异常规则列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryStr", value = "查询关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "stationTypeId", value = "电站类型ID", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "stationId", value = "电站ID", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "alarmLevelId", value = "异常级别ID", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "deviceTypeId", value = "设备类型ID", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "alarmTypeId", value = "异常类型ID", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "alarmScopeId", value = "作用范围ID", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "维护时间", dataType = "string", paramType = "query")
    })
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<ResultObject> search(@RequestParam("queryStr") Optional<String> queryStr,
                                               @RequestParam("stationTypeId") Optional<List<Long>> stationTypeId,
                                               @RequestParam("stationId") Optional<List<Long>> stationId,
                                               @RequestParam("alarmLevelId") Optional<List<Long>> alarmLevelId,
                                               @RequestParam("deviceTypeId") Optional<List<Long>> deviceTypeId,
                                               @RequestParam("alarmTypeId") Optional<List<Long>> alarmTypeId,
                                               @RequestParam("alarmScopeId") Optional<Long> alarmScopeId,
                                               @RequestParam("dtime") Optional<List<String>> dtime,
                                               @ModelAttribute PageParam pageParam) {
        Iterable<AlarmRuleDTO> results = alarmRuleService.search(queryStr.orElse(null), stationTypeId.orElse(null),
                stationId.orElse(null), alarmLevelId.orElse(null), deviceTypeId.orElse(null), alarmTypeId.orElse(null),
                alarmScopeId.orElse(null), dtime.orElse(null), pageParam);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    /**
     * 公式检查
     * @param formula 公式
     */
    @ApiOperation("公式检查")
    @ApiImplicitParam(name = "formula", value = "公式", dataType = "string", paramType = "query")
    @RequestMapping(value = "/formulaCheck", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> formulaCheck(@RequestParam("formula") String formula) throws ArgumentErrorException {
        boolean result = alarmRuleService.formulaCheck(formula);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    /**
     * 异常规则下发到DPU
     */
    @ApiOperation("异常规则下发到DPU")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "电站ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "alarmRuleInstanceIds", value = "告警规则实例ID列表", dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/sendAlarmRuleInstancesToDpu", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> sendAlarmRuleInstancesToDpu(@RequestParam("stationId") Optional<Long> stationId,
                                                                    @RequestParam("alarmRuleInstanceIds") Optional<List<Long>> alarmRuleInstanceIds)
            throws JsonProcessingException, ArgumentErrorException {
        boolean result = alarmRuleService.sendAlarmRulesToDpu(stationId, alarmRuleInstanceIds);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }
}
